package live.jialing.sip.contact.student.service;

import com.google.common.collect.Lists;
import live.jialing.core.data.LinkType;
import live.jialing.core.data.jpa.CustomJpaRepository;
import live.jialing.core.service.BaseDomainService;
import live.jialing.core.service.GeneralService;
import live.jialing.data.util.SpecificationUtil;
import live.jialing.sip.base.domain.Student;
import live.jialing.sip.contact.student.domain.StudentGroup;
import live.jialing.sip.contact.student.repository.StudentGroupRepository;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * 家校通讯录服务
 */
@Service
@Slf4j
public class StudentContactListService extends BaseDomainService<StudentGroup, Long> implements GeneralService<StudentGroup, Long> {

    @Resource
    private StudentGroupRepository studentGroupRepository;

    @Override
    public CustomJpaRepository getRepository() {
        return this.studentGroupRepository;
    }

    public Specification<StudentGroup> buildSpecification(Class<StudentGroup> classz, Map<String, Object> searchParams, LinkType type) {

        return SpecificationUtil.buildSpecification(classz, searchParams, type);
    }

    /**
     * 获取组内的学生
     *
     * @param groupId
     * @return
     */
    public List<Student> findGroupStudentByGroupId(long groupId) {

        if (groupId < 1) {
            return Lists.newArrayList();
        }

        StudentGroup studentGroup = studentGroupRepository.findGroupFetchStudentByPK(groupId);
        if (studentGroup == null) {
            return Lists.newArrayList();
        }

        return studentGroup.getStudents();
    }
}
